/**
 * Copyright 2007 Charlie Hubbard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package flexjson;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class ChainedSet implements Set {
    Set parent;
    Set child;

    public ChainedSet(Set parent) {
        this.parent = parent;
        this.child = new HashSet();
    }

    public int size() {
        return this.child.size() + parent.size();
    }

    public boolean isEmpty() {
        return this.child.isEmpty() && parent.isEmpty();
    }

    public boolean contains(Object o) {
        return child.contains(o) || parent.contains(o);
    }

    public Iterator iterator() {
        return new ChainedIterator(child, parent);
    }

    public Object[] toArray() {
        Object[] carr = child.toArray();
        Object[] parr = parent.toArray();
        Object[] combined = new Object[carr.length + parr.length];
        System.arraycopy(carr, 0, combined, 0, carr.length);
        System.arraycopy(parr, 0, combined, carr.length, parr.length);
        return combined;
    }

    public Object[] toArray(Object[] a) {
        throw new IllegalStateException("Not implemeneted");
    }

    public boolean add(Object o) {
        return child.add(o);
    }

    public boolean remove(Object o) {
        return child.remove(o);
    }

    public boolean containsAll(Collection c) {
        return child.containsAll(c) || parent.containsAll(c);
    }

    public boolean addAll(Collection c) {
        return child.addAll(c);
    }

    public boolean retainAll(Collection c) {
        return child.retainAll(c);
    }

    public boolean removeAll(Collection c) {
        return child.removeAll(c);
    }

    public void clear() {
        child.clear();
    }

    public Set getParent() {
        return parent;
    }
}
